<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-col :xl="6" :lg="7" :md="8" :sm="24">
          <a-form-item label="店铺名称">
            <a-input placeholder="输入店铺名称查询" v-model="queryParam.shopName"></a-input>
          </a-form-item>
        </a-col>
        <a-col :xl="6" :lg="7" :md="8" :sm="24">
          <a-form-item label="商品名称">
            <a-input placeholder="输入商品名称查询" v-model="queryParam.name"></a-input>
          </a-form-item>
        </a-col>
        <a-col :xl="6" :lg="7" :md="8" :sm="24">
          <a-form-item label="上下架状态">
            <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.status" dictCode="mk_goods_status" />
          </a-form-item>
        </a-col>
        <a-col :xl="6" :lg="7" :md="8" :sm="24">
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            <a @click="handleToggleSearch" style="margin-left: 8px">
              {{ toggleSearchStatus ? '收起' : '展开' }}
              <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
            </a>
          </span>
        </a-col>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="showhandleAddto=true" type="primary" icon="plus">新增</a-button>
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery">
      </j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns" :dataSource="dataSource" :pagination="ipagination" :loading="loading" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap" @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEditto(record)">编辑</a>

          <a-divider type="vertical" />
          <!--           <a @click="handleDetail(record)">详情</a>-->
          <a-dropdown>
            <a class="ant-dropdown-link">更多
              <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <GoodsBasicModal ref="modalForm" @ok="modalFormOk"></GoodsBasicModal>
    <j-modal title="兑换详情" width="800px" :visible="showhandleAddto" switchFullscreen @ok="handleAddto" @cancel="showhandleAddto=false" cancelText="关闭">
      <a-form-model>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="商品全称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入商品全称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="库存" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="originalPrice">
              <a-input-number v-model="model.stock" placeholder="请输入库存" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="兑换点数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="originalPrice">
              <a-input-number v-model="model.spendPoint" placeholder="请输入兑换点数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="picture">
              <j-image-upload biz-path="laile2/goods" acceptFormat="image/png, image/jpeg" v-model="model.image">
              </j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.shortName" placeholder="请输入商品简称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.topic" placeholder="请输入商品描述"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="mk_goods_status" placeholder="请选择商品上下架状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="originalPrice">
              <a-input-number v-model="model.sort" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-modal>
  </a-card>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import GoodsBasicModal from './modules/GoodsBasicModal.vue'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { postAction } from '../../api/manage'

export default {
  name: 'GoodsBasicList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    GoodsBasicModal
  },
  data() {
    return {
      libraryData: [],
      ipagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        total: 0
      },
      selectedRowKeysArrey: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      message: "hello vue",
      shopList: [],
      description: '商品表管理页面',
      // 表头
      columns: [
        {
          title: '商品全称',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '兑换点数',
          align: 'center',
          dataIndex: 'spendPoint'
        },
        {
          title: '商品状态',
          align: 'center',
          dataIndex: 'status',
          customRender: function (text) {
            if (text == 1) {
              return '正常'
            } else if (text == 2) {
              return '下架'
            } else {
              return text
            }
          }
        },
        {
          title: '排序(从大到小)',
          align: 'center',
          dataIndex: 'sort'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      model: { shopId: '1507610418441297922' },
      queryParam: {
        goodBrand: localStorage.getItem('myDataId')
      },
      url: {
        list: '/exchangeGoodBase/mkExchangeGoodBase/list',
        delete: '/exchangeGoodBase/mkExchangeGoodBase/delete',
        deleteBatch: '/exchangeGoodBase/mkExchangeGoodBase/deleteBatch',
        exportXlsUrl: '/goods/goodsBasic/exportXls',
        importExcelUrl: 'goods/goodsBasic/importExcel',
        statusChangeUrl: '/goods/goodsBasic/statusChange',
        isIndexChangeUrl: '/goods/goodsBasic/isIndexChange',
        isHotChangeUrl: '/goods/goodsBasic/isHotChange',
        delFlagChangeUrl: '/goods/goodsBasic/delFlagChange',
        getShopListUrl: '/shop/shopBasic/selectShopList',
        add: '/exchangeGoodBase/mkExchangeGoodBase/add',
      },
      dictOptions: {},
      superFieldList: [],
      showhandleAddto: false,
    }
  },
  created() {
    this.getShopListUrl()
    this.getSuperFieldList()
    this.localStorage()
    // // 从本地缓存获取数据
    // const storedData = localStorage.getItem('myDataId');
    // console.log(storedData);
    // if (storedData) {
    //   this.queryParam.goodBrand = storedData
    // }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleEditto(e) {
      this.model = e
      this.showhandleAddto = true
    },
    changePage(pagination) {
      this.queryParam.pageNo = pagination.current;
      this.queryParam.pageSize = pagination.pageSize;
    },
    // 新增
    handleAddto() {
      if (this.model.id) {
        postAction("/exchangeGoodBase/mkExchangeGoodBase/edit", this.model).then(res => {
          console.log(res)
          if (res.success) {
            this.showhandleAddto = false
            this.model = {}
            this.searchQuery()
            this.$message.success('成功')
          }
        })
      } else {
        postAction(this.url.add, this.model).then(res => {
          console.log(res)
          if (res.success) {
            this.showhandleAddto = false
            this.model = {}
            this.searchQuery()
            this.$message.success('成功')

          }
        })
      }
    },
    localStorage() {
      localStorage.setItem("library", 2);
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'shopId', text: '所属门店', dictCode: 'mk_shop_basic,name,id' })
      fieldList.push({ type: 'string', value: 'name', text: '商品全称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'shortName', text: '商品简称', dictCode: '' })
      fieldList.push({
        type: 'list_multi',
        value: 'sorts',
        text: '商品分类',
        dictTable: 'mk_goods_sort',
        dictText: 'name',
        dictCode: 'id'
      })
      fieldList.push({ type: 'BigDecimal', value: 'originalPrice', text: '商品原价', dictCode: '' })
      fieldList.push({ type: 'string', value: 'presentPrice', text: '商品现价', dictCode: '' })
      fieldList.push({ type: 'string', value: 'code', text: '商品代码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'picture', text: '商品图片', dictCode: '' })
      fieldList.push({ type: 'string', value: 'description', text: '商品完整描述', dictCode: '' })
      fieldList.push({ type: 'string', value: 'shortDescription', text: '商品简单描述', dictCode: '' })
      fieldList.push({ type: 'string', value: 'status', text: '商品上下架状态', dictCode: 'mk_goods_status' })
      fieldList.push({ type: 'string', value: 'isIndex', text: '是否首页展示', dictCode: 'mk_whether' })
      fieldList.push({ type: 'string', value: 'isHot', text: '是否热门产品', dictCode: 'mk_whether' })
      fieldList.push({ type: 'int', value: 'visitNum', text: '浏览次数', dictCode: '' })
      fieldList.push({ type: 'int', value: 'saleNum', text: '销售数量', dictCode: '' })
      fieldList.push({ type: 'string', value: 'delFlag', text: '是否删除', dictCode: 'mk_whether' })
      fieldList.push({ type: 'int', value: 'sort', text: '排序', dictCode: '' })
      this.superFieldList = fieldList
    },
    start() {
      this.loading = true
      // ajax request after empty completing
      setTimeout(() => {
        this.loading = false
        this.selectedRowKeys = []
      }, 1000)
    },
    getShopListUrl() {
      this.shopList = []
      getAction(this.url.getShopListUrl).then(res => {
        if (res.success) {
          let result = res.result
          result.forEach((e) => {
            this.shopList.push(e)
          })
        }
      })
      console.log(this.shopList)

    },
    onChangeStatus(record) {
      console.log(record)
      var that = this
      that.loading = true
      getAction(this.url.statusChangeUrl + '/' + record.id).then(res => {
        if (res.success) {
          //重新计算分页问题
          that.reCalculatePage(that.selectedRowKeys.length)
          that.$message.success(res.message)
          that.loadData()
          that.onClearSelected()
        } else {
          that.$message.warning('操作异常,请稍后再试')
        }
      }).finally(() => {
        that.loading = false
      })
    },
    onChangeIsIndex(record) {
      console.log(record)
      var that = this
      that.loading = true
      getAction(this.url.isIndexChangeUrl + '/' + record.id).then(res => {
        if (res.success) {
          //重新计算分页问题
          that.reCalculatePage(that.selectedRowKeys.length)
          that.$message.success(res.message)
          that.loadData()
          that.onClearSelected()

        } else {
          that.$message.warning('操作异常,请稍后再试')
        }
      }).finally(() => {
        that.loading = false
      })

    },
    onChangeIsHot(record) {
      console.log(record)
      var that = this
      that.loading = true
      getAction(this.url.isHotChangeUrl + '/' + record.id).then(res => {
        if (res.success) {
          //重新计算分页问题
          record = res.result
          that.reCalculatePage(that.selectedRowKeys.length)
          that.$message.success(res.message)
          that.loadData()
          that.onClearSelected()

        } else {
          that.$message.warning('操作异常,请稍后再试')
        }
      }).finally(() => {
        that.loading = false
      })

    }
    /*onChangeDelFlag(record) {
        var that = this;
        that.loading = true;
        getAction(this.url.delFlagChangeUrl+"/"+record.id).then(res => {
            if (res.success) {
                record.delFlag = res.result.delFlag;
                //重新计算分页问题
                that.reCalculatePage(that.selectedRowKeys.length)
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
            } else {
                that.$message.warning("操作异常,请稍后再试");
            }
        }).finally(() => {
            that.loading = false;
        });
    },*/
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>